package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.SmallPageBean;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    //注入dao
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        //先去添加基本信息
        checkGroupDao.add(checkGroup);
        //获取主键自增后的值，操作中间表新增时用
        //设置检查组和检查项的多对多的关联关系，操作t_checkgroup_checkitem表
        Integer checkgroupId = checkGroup.getId();

        this.setCheckGroupAndCheckItem(checkgroupId,checkitemIds);
    }

    //操作检查项和检查组多对多的关联关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkItemIds) {
        if (checkItemIds != null && checkItemIds.length > 0 ) {
            //循环遍历---拿出每个checkItemId值
            for (Integer checkItemId : checkItemIds) {
                //封装到map集合，sql语句中的参数就是map类型，键值对
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkItemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页显示几条数据
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();

        //分页助手插件
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckGroup> list = checkGroupDao.findByCondition(queryString);

        //总条数
        long total = list.getTotal();
        //全部数据
        List<CheckGroup> result = list.getResult();

        return new PageResult(total,result);
    }

    @Override
    public void deleteById(Integer id) {
        long count = checkGroupDao.findCountByCheckGroupId(id);
        if (count > 0){
            throw new CheckItemDeleteFailException();
        }
        checkGroupDao.deleteById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup,Integer[] checkItemIds) {
        //先修改
        checkGroupDao.edit(checkGroup);
        //删除中间表现有的连接,根据检查组ID清理关联的检查项信息
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.deleteAssocication(checkGroupId);

        //在重新往中间表中添加数据
        this.setCheckGroupAndCheckItem(checkGroupId,checkItemIds);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return null;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(String checkGroupId) {
        /*//当前页
        Integer smallCurrentPage = smallPageBean.getSmallCurrentPage();
        //每页显示多少内容
        Integer smallPageSize = smallPageBean.getSmallPageSize();

        //分页插件助手
        //PageHelper.startPage(smallCurrentPage,smallPageSize);*/
        //执行sql语句
        List<Integer> checkItemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);

        /*//总条数
        long total = checkItemIds.getTotal();
        //所有的ID
        List result = checkItemIds.getResult();*/
        return checkItemIds;
    }

    //小型分页查询
    @Override
    public PageResult findAll(SmallPageBean smallPageBean) {
        //当前页
        Integer smallCurrentPage = smallPageBean.getSmallCurrentPage();
        //每页显示的条数
        Integer smallPageSize = smallPageBean.getSmallPageSize();

        //分页插件助手
        PageHelper.startPage(smallCurrentPage,smallPageSize);
        Page<CheckGroup> checkGroups = checkGroupDao.findAll();

        //总条数
        long total = checkGroups.getTotal();
        //所有的ID
        List result = checkGroups.getResult();
        return new PageResult(total,result);
    }


}
